package com.projiect.dao.impl;

import com.projiect.bean.ClassBean;
import com.projiect.dao.IClassBeanDao;

public class IClassBeanDaoImpl extends BaseDao implements IClassBeanDao {
    @Override
    public void createClassTable() {
        this.setCon();
        try {
            ps = con.prepareStatement("CREATE TABLE t_class(" +
                    "pk_classId INT PRIMARY KEY AUTO_INCREMENT," +
                    "c_className VARCHAR(20)" +
                    ");");

            ps.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.close();
        }
    }

    @Override
    public void addClassBean(ClassBean classBean) {
        this.setCon();
        try {
            ps = con.prepareStatement("insert into t_class(c_className) values (?)");

            ps.setObject(1, classBean.getClassName());

            ps.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.close();
        }
    }


    @Override
    public ClassBean findClassById(int id) {
        this.setCon();
        try {
            ps = con.prepareStatement("select * from t_class where pk_classId=? ");
            ps.setObject(1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                ClassBean classBean = new ClassBean();
                classBean.setId(rs.getInt("pk_classId"));
                classBean.setClassName(rs.getString("c_className"));
                return classBean;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.close();
        }
        return null;
    }
}
